import numpy as np
import matplotlib.pyplot as plt
import keras
from keras import layers

center_x = 50
center_y = 50
radius1 = np.random.randint(40,50,(1000,))
degree1 = np.random.randint(0,360,(1000,)) / 180 * np.pi

x1 = np.cos(degree1) * radius1 + center_x
y1 = np.sin(degree1) * radius1 + center_y

radius2 = np.random.randint(0,30,(1000,))
degree2 = np.random.randint(0,360,(1000,)) / 180 * np.pi

x2 = np.cos(degree2) * radius2 + center_x
y2 = np.sin(degree2) * radius2 + center_y

plt.plot(x1,y1,'ro')
plt.plot(x2,y2,'bo')
plt.xlim(0,100)
plt.ylim(0,100)

# ------------- 组合散点 ------------------
x1_tmp = np.array(x1,ndmin=2)
y1_tmp = np.array(y1,ndmin=2)
x1y1 = np.stack((x1_tmp,y1_tmp),axis=2)[0]
z1 = np.zeros(x1.shape)

x2_tmp = np.array(x2,ndmin=2)
y2_tmp = np.array(y2,ndmin=2)
x2y2 = np.stack((x2_tmp,y2_tmp),axis=2)[0]
z2 = np.ones(x2.shape)

# ---------------- 数据集 ---------------------

train_data = np.concatenate((x1y1,x2y2),axis=0)
train_labels = np.concatenate((z1,z2),axis=0).reshape(-1, 1)
# 将分类信息标注出来
train_labels = keras.utils.to_categorical(train_labels,num_classes=2)

# ----------------- 构建神经网络做分类训练 -------------

model = keras.Sequential([
    layers.Flatten(input_shape=(2,)),
    layers.Dense(10,activation="relu"),
    layers.Dense(10,activation="relu"),
    layers.Dense(10,activation="relu"),
    layers.Dense(2,activation="softmax") # 结果是一个二分类
])

model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=0.01),
    loss=keras.losses.binary_crossentropy, # 损失率
    # accuracy 准确率
    metrics=["acc"]
)

# model.fit(
#     train_data,train_labels,
#     epochs=1000,
#     validation_split=0.2)
#
# # 将训练结果保存起来 注意保存的模型的格式是“.keras”
# # model.save('model2.keras')
#
# # predict_labels = model.predict(train_data)
#
# # 通过argmax 将一行中的最大概率的分类的下标
# # result = np.argmax(predict_labels,axis=1)
# # print(result)
#
plt.show()